<template>
  <div>
    <!-- 在右上角添加子系统管理文本 -->
    <div class="subsystem-manage-title">子系统管理</div>

    <div class="form-container">
      <!-- 添加子系统按钮 -->
      <el-button type="primary" @click="openAddDialog">增加子系统</el-button>
      <!-- 删除子系统按钮 -->
      <el-button type="danger" @click="confirmDelete">删除子系统</el-button>
      <!-- 搜索输入框：按名称搜索 -->
      <el-input placeholder="搜索名称" v-model="search.name" class="search-input"></el-input>
      <!-- 搜索输入框：按代码搜索 -->
      <el-input placeholder="搜索代码" v-model="search.code" class="search-input"></el-input>
      <!-- 搜索输入框：按备注搜索 -->
      <el-input placeholder="搜索备注" v-model="search.description" class="search-input"></el-input>
      <!-- 查询按钮 -->
      <el-button type="primary" @click="fetchSubsystems">查询</el-button>
    </div>
    <!-- 子系统表格 -->
    <el-table :data="subsystems" style="width: 100%" ref="subTable" highlight-current-row>
      <!-- 表格列：选择框 -->
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- 表格列：名称 -->
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <!-- 表格列：代码 -->
      <el-table-column prop="code" label="代码" width="180"></el-table-column>
      <!-- 表格列：访问链接 -->
      <el-table-column prop="url" label="访问链接"></el-table-column>
      <!-- 表格列：备注 -->
      <el-table-column prop="description" label="备注"></el-table-column>
      <!-- 表格列：操作 -->
      <el-table-column label="操作">
        <template #default="scope">
          <!-- 编辑按钮 -->
          <el-button size="mini" @click="openEditDialog(scope.row)">编辑</el-button>
          <!-- 复制按钮 -->
          <el-button size="mini" @click="openCopyDialog(scope.row)">复制</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框：用于添加、编辑和复制子系统 -->
    <el-dialog v-model:visible="isDialogOpen" :title="dialogTitle">
      <!-- 表单 -->
      <el-form :model="form">
        <!-- 表单项：子系统名称 -->
        <el-form-item label="子系统名称" required>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <!-- 表单项：子系统代码 -->
        <el-form-item label="子系统代码" required>
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <!-- 表单项：子系统访问链接 -->
        <el-form-item label="子系统访问链接">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <!-- 表单项：子系统图标 -->
        <el-form-item label="子系统图标">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <!-- 表单项：备注 -->
        <el-form-item label="备注">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框底部按钮 -->
      <template #footer>
        <!-- 取消按钮 -->
        <el-button @click="isDialogOpen = false">取消</el-button>
        <!-- 确定按钮 -->
        <el-button type="primary" @click="saveSubsystem">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.toolbar {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}
.search-input {
  width: 200px;
  margin-right: 10px;
}
.subsystem-manage-title {
  position: absolute;
  top: 10px;
  left: 10px;
  color: blue;
  font-size: 30px;
  font-weight: bold;
  margin-top: 10px;
}
.form-container {
  margin-top: 100px; /* 下移整个表单 */
}
</style>

<script>
export default {
  name: "SubsystemManage",
  data() {
    return {
      subsystems: [],
      allSubsystems: [],
      isDialogOpen: false,
      dialogTitle: "添加子系统",
      form: {
        name: '',
        code: '',
        url: '',
        icon: '',
        description: '',
        status: '启用'
      },
      search: {
        name: '',
        code: '',
        description: ''
      },
      initialized: false
    };
  },
  methods: {
    openAddDialog() {
      this.form = { name: '', code: '', url: '', icon: '', description: '', status: '启用' };
      this.dialogTitle = "添加子系统";
      this.isDialogOpen = true;
    },
    openEditDialog(subsystem) {
      this.form = { ...subsystem };
      this.dialogTitle = "编辑子系统";
      this.isDialogOpen = true;
    },
    openCopyDialog(subsystem) {
      this.form = { ...subsystem, code: '' };
      this.dialogTitle = "复制子系统";
      this.isDialogOpen = true;
    },
    saveSubsystem() {
      console.log('Saving data', this.form);
      if (this.dialogTitle === "添加子系统") {
        this.allSubsystems.unshift({ ...this.form, id: Date.now() });
        this.$message.success('添加子系统成功');
      } else if (this.dialogTitle === "编辑子系统") {
        const index = this.allSubsystems.findIndex(sub => sub.id === this.form.id);
        if (index !== -1) {
          this.allSubsystems.splice(index, 1);
          this.allSubsystems.unshift({ ...this.form });
          this.$message.success('编辑子系统成功');
        }
      }
      this.applySearch();
      this.isDialogOpen = false;
    },
    confirmDelete() {
      this.$confirm('确定删除选中的子系统吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteSubsystems();
      }).catch(() => {});
    },
    handleSearch() {
      this.applySearch();
    },
    fetchSubsystems() {
      console.log('Fetching data for', this.search);
      this.applySearch();
    },
    applySearch() {
      const { name, code, description } = this.search;
      this.subsystems = this.allSubsystems.filter(subsystem => {
        return (!name || subsystem.name.includes(name)) &&
            (!code || subsystem.code.includes(code)) &&
            (!description || subsystem.description.includes(description));
      });
    },
    deleteSubsystems() {
      const selectedIds = this.$refs.subTable.selection.map(item => item.id);
      console.log('Deleting subsystems', selectedIds);
      this.allSubsystems = this.allSubsystems.filter(subsystem => !selectedIds.includes(subsystem.id));
      this.applySearch();
      this.$message.success('删除成功');
    },
    initializeSubsystems() {
      if (!this.initialized) {
        this.allSubsystems = [
          { id: 1, name: 'Example 1', code: 'EX001', url: 'http://example1.com' },
          { id: 2, name: 'Example 2', code: 'EX002', url: 'http://example2.com' },
          { id: 3, name: 'Example 3', code: 'EX003', url: 'http://example3.com' },
          { id: 4, name: 'Example 4', code: 'EX004', url: 'http://example4.com' },
          { id: 5, name: 'Example 5', code: 'EX005', url: 'http://example5.com' }
        ];
        this.initialized = true;
      }
      this.applySearch();
    }
  },
  mounted() {
    this.initializeSubsystems();
  }
}
</script>